package com.csj.DesignPattern.flyweight;

/*
    享元模式：用于需要重复使用的对象时
 */
public class Client {
    public static void main(String[] args) {
        BoxFactory factory = BoxFactory.getInstance();
        AbstractBox box1 = factory.getShape("I");
        box1.display("red");

        AbstractBox box2 = factory.getShape("L");
        box2.display("blue");

        AbstractBox box3 = factory.getShape("O");
        box3.display("green");

        AbstractBox box4 = factory.getShape("O");
        box4.display("yellow");

        System.out.println(box3 == box4);




        System.out.println("================================");


        Integer i1 = 127; // Integer i1 = Integer.valueOf((int)127);
        Integer i2 = 127;
        System.out.println(i1 == i2);
        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println(i3 == i4);

        /*
            此处IntegerCache.low为-128  IntegerCache.high为127
            public static Integer valueOf(int i) {
                if (i >= IntegerCache.low && i <= IntegerCache.high)
                    return IntegerCache.cache[i + (-IntegerCache.low)];
                return new Integer(i);
            }
            此处的IntegerCache.cache[]就是本案例中BoxFactory中的map，相当于一个缓存
            当数字在-128~127之间时，会直接从缓存中取(取出来的对象都是相同的)，否则会new一个Integer对象
         */
    }
}
